//
//  ZDDPictureInfo.swift
//  小蜗牛_新浪微博
//
//  Created by 周冬冬 on 16/6/30.
//  Copyright © 2016年 我的mac. All rights reserved.
//

import UIKit
//cell之间的间距
private let itemMargin:CGFloat = 5

//cell的宽高
private let cellWH = (kScreenW - 2 * StautsMargin - itemMargin*2)/3
private let pictureViewCellId = "pictureViewCellId"
class ZDDPictureInfo: UICollectionView {

    var pic_urls:[ZDDPictureModel]?{
    
        didSet{
         
        countLabel.text = "\(pic_urls?.count ?? 0)"
       let size = pictureViewSize(pic_urls?.count ?? 0)
       self.snp_updateConstraints { (make) -> Void in
        make.size.equalTo(size)
            }
            reloadData()
        }
    
    }
    
    func pictureViewSize(count:Int)->CGSize{
    
      
        
        //计算行和列
        let col = count == 4 ? 2 : count >= 3 ? 3 : count
        let row = count == 4 ? 2 : ((count - 1)/3 + 1)
        //计算配图的宽高
        let w = CGFloat(col) * cellWH + CGFloat(col - 1) * itemMargin
        let h = CGFloat(row) * cellWH + CGFloat(row - 1) * itemMargin
        
        return CGSize(width: w, height: h)
    
    }
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        super.init(frame: frame, collectionViewLayout: UICollectionViewFlowLayout())
      
        setupUI()
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI(){
    
        backgroundColor = UIColor.clearColor()
        //注册
        registerClass(ZDDPictureCell.self, forCellWithReuseIdentifier: pictureViewCellId)
       //添加控件
        addSubview(countLabel)
        let layout = collectionViewLayout as! UICollectionViewFlowLayout
        
        //设置item
        layout.itemSize = CGSize(width: cellWH, height: cellWH)
        //设置间距
        layout.minimumInteritemSpacing = itemMargin
        layout.minimumLineSpacing = itemMargin
        dataSource = self
        //添加约束
        countLabel.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
        }
    
        
    }
    
    //懒加载控件
    
    private lazy var countLabel :UILabel = UILabel(textColor: ThemeColor, textFont: 35)
}
extension ZDDPictureInfo:UICollectionViewDataSource{

    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return pic_urls?.count ?? 0
        
    }
    

    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(pictureViewCellId, forIndexPath: indexPath) as! ZDDPictureCell
//        cell.backgroundColor = RandomColor()
        //赋值
        cell.pictureInfo = pic_urls![indexPath.item]
        return cell
        
    }




}
class ZDDPictureCell: UICollectionViewCell {
    var pictureInfo:ZDDPictureModel?{
        
        didSet{
            
            bgImageView.sd_setImageWithURL(NSURL(string:pictureInfo?.thumbnail_pic ?? ""), placeholderImage: UIImage(named: "timeline_card_top_background"))
        }
        }
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    func setupUI(){
    
        
        backgroundColor = RandomColor()
    
        contentView.addSubview(bgImageView)
    
        //设置约束
        bgImageView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(contentView).offset(UIEdgeInsetsZero)
        }
    
    }
    
     //  MARK: - 懒加载控件
    private lazy var bgImageView : UIImageView = {
    
        let img = UIImageView(imageName: "timeline_card_top_background")
        
        //设置图片的填充方式
        img.contentMode = .ScaleAspectFill
        
        img.clipsToBounds = true
    
        return img
    
    }()

}







